package top.ajunnihao.core.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.ajunnihao.core.entity.Order;
import top.ajunnihao.core.service.OrderService;
import top.ajunnihao.core.service.SiteScenicService;
import top.ajunnihao.core.service.UserService;
import top.ajunnihao.core.util.resp.JResp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 统计数据前端控制器
 * </p>
 */
@Controller
public class CountController {

    @Autowired
    SiteScenicService siteScenicService;
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;

    @GetMapping("/admin/count")
    @ResponseBody
    public Object countRest() {
        return JResp.ok(getCountMap());
    }

    @GetMapping({"/admin", "/admin/", "/admin/index"})
    public String countMvc(Model model) {
        model.addAttribute("countData", getCountMap());
        return "admin/dashboard";
    }

    private Map<String, Integer> getCountMap() {
        int siteCount = siteScenicService.count();
        int userCount = userService.count();
        int orderCount = orderService.count();
        List<Order> orderList = orderService.list();
        int ticketingCount = 0;
        for (Order order : orderList) {
            ticketingCount += order.getOrderCount();
        }
        Map<String, Integer> countData = new HashMap<>();
        countData.put("siteCount", siteCount);
        countData.put("userCount", userCount);
        countData.put("orderCount", orderCount);
        countData.put("ticketingCount", ticketingCount);
        return countData;
    }

}
